package service;

import dao.PermissionDao;
import dao.impl.PermissionDaoImpl;
import org.apache.commons.beanutils.BeanUtils;
import pojo.Department;
import pojo.Permission;
import util.Result;
import util.StringDataCheckUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

public class PermissionService {

    private PermissionDao permissionDao = new PermissionDaoImpl();

    /**
     * 展示所有带条件查找的权限
     *
     * @param request
     * @param response
     */
    public void permissionListByParam(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Permission permission = new Permission();
            BeanUtils.populate(permission, parameterMap);
            List<Permission> permissionList = permissionDao.listPermissionByParam(permission);
            if (permissionList != null) {
                Result.response(200, "查找成功！", response, permissionList);
            } else {
                Result.response(1001, "查找失败！", response);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过职位查找权限
     *
     * @param request
     * @param response
     */
    public void findPermissionByDC(HttpServletRequest request, HttpServletResponse response) {

        String departmentCode = request.getParameter("departmentCode");
        if (!StringDataCheckUtil.check(departmentCode)) {
            Result.response(1001, "查找失败！", response);
            return;
        }
        if (!"0".equals(departmentCode)){
            Department permission = permissionDao.findPermissionByDC(departmentCode);
            String departmentPermission = permission.getDepartmentPermission();
            String[] splits = departmentPermission.split("-");
            Result.response(200, "查找成功!", response, splits);
        }
    }

    /**
     * 编辑权限信息
     * @param request
     * @param response
     */
    public void editPermission(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        String departmentPermission = "";
        String departmentCode = "";
        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
            if (stringEntry.getKey().contains("permission")){
                departmentPermission += stringEntry.getValue()[0]+"-";
            }
            if ("keywordDepartmentCode".equals(stringEntry.getKey())){
                departmentCode = stringEntry.getValue()[0];
            }
        }
        departmentPermission = departmentPermission.substring(0, departmentPermission.length()-1);

        if (permissionDao.editPermission(departmentCode, departmentPermission) > 0) {
            Result.response(200, "修改成功！", response);
        }else {
            Result.response(1001, "修改失败！", response);
        }

    }
}
